package ee.ut.cs.mc.l2;

import java.io.Serializable;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.Button;

/**
 * @author Kaarel Jõgeva
 */
public class MainActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        addOnClick(R.id.startFlagBtn, Flags.class, null);
        addOnClick(R.id.startGreeterBtn, Greeter.class, null);
        addOnClick(R.id.startConverterBtn, Converter.class, null);
        addOnClick(R.id.startIntentExtraBtn, IntentExtra.class, "Testing extra value!");
        addOnClick(R.id.sendEmailBtn, null, null);
    }

    private void addOnClick(int id, final Class<? extends Activity> activityClass, final Serializable extraValue) {
        Button flagBtn = (Button) findViewById(id);
        flagBtn.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                startActivity(createIntent(activityClass, extraValue));
            }
        });
    }

    private Intent createIntent(Class<? extends Activity> activityClass, Serializable extraValue) {
        if (activityClass == null) {
            Intent intent = new Intent(Intent.ACTION_SEND);
            intent.setType("plain/text");
            return intent.putExtra(Intent.EXTRA_EMAIL, new String[] { "leraak@gmail.com" }).putExtra(Intent.EXTRA_SUBJECT, "Testmail");
        }

        Intent intent = new Intent(this, activityClass);
        if (extraValue == null) {
            return intent;
        }

        return intent.putExtra("extraInformation", extraValue);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
}
